package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerfilestandardcolumnController")
public class CrawlerfilestandardcolumnController extends CmiEmaController {

	@RequestMapping("/listCrawlerfilestandardcolumns")
	public ModelAndView listCrawlerfilestandardcolumns(HttpServletRequest request,
			@ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilestandardcolumnPaginationService.updatePagination(request, pagination);
		Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumns = crawlerfilestandardcolumnPaginationService
				.getPaginationCrawlerfilestandardcolumns(pagination);

		mav.addObject("crawlerfilestandardcolumns", crawlerfilestandardcolumns);
		mav.addObject("entityName", "crawlerfilestandardcolumn");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfilestandardcolumn/listCrawlerfilestandardcolumns.jsp");
		return mav;
	}

	public String listCrawlerfilestandardcolumns() {
		return "redirect:/listCrawlerfilestandardcolumns";
	}

	@RequestMapping("/viewCrawlerfilestandardcolumn")
	public ModelAndView viewCrawlerfilestandardcolumn(@RequestParam Integer crawlerfilestandardcolumnId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfilestandardcolumn",
				crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId));
		mav.setViewName("crawlerfilestandardcolumn/viewCrawlerfilestandardcolumn.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfilestandardcolumns")
	public ModelAndView confirmDeleteCrawlerfilestandardcolumns(
			@RequestParam(value = "deleteCrawlerfilestandardcolumnsIds", required = true) Set<Integer> deleteCrawlerfilestandardcolumnsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfilestandardcolumn> deleteCrawlerfilestandardcolumns = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnsByIds(deleteCrawlerfilestandardcolumnsIds);
		Set<Crawlerfilestandardcolumn> referencedCrawlerfilestandardcolumns = crawlerfilestandardcolumnService
				.loadReferenceCrawlerfilestandardcolumns(deleteCrawlerfilestandardcolumns);
		boolean hasReferenced;
		if (referencedCrawlerfilestandardcolumns.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfilestandardcolumns);
			hasReferenced = false;
		} else {
			deleteCrawlerfilestandardcolumns.removeAll(referencedCrawlerfilestandardcolumns);
			mav.addObject("referencedEntitys", referencedCrawlerfilestandardcolumns);
			mav.addObject("deleteEntitys", deleteCrawlerfilestandardcolumns);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfilestandardcolumn");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfilestandardcolumns");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandardcolumn")
	public String deleteCrawlerfilestandardcolumn(@RequestParam Integer crawlerfilestandardcolumnId) {
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId);
		crawlerfilestandardcolumnService.deleteCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		return "forward:/listCrawlerfilestandardcolumns";
	}

	@RequestMapping("/confirmDeleteCrawlerfilestandardcolumn")
	public ModelAndView confirmDeleteCrawlerfilestandardcolumn(@RequestParam Integer crawlerfilestandardcolumnId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfilestandardcolumn",
				crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId));

		mav.setViewName("crawlerfilestandardcolumn/deleteCrawlerfilestandardcolumn.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandardcolumns")
	public String deleteCrawlerfilestandardcolumns(
			@RequestParam(value = "deleteCrawlerfilestandardcolumnsId", required = true) Set<Integer> deleteCrawlerfilestandardcolumnsId) {
		crawlerfilestandardcolumnService.deleteCrawlerfilestandardcolumns(deleteCrawlerfilestandardcolumnsId);
		return "forward:/listCrawlerfilestandardcolumns";
	}

	@RequestMapping("/editCrawlerfilestandardcolumn")
	public ModelAndView editCrawlerfilestandardcolumn(@RequestParam Integer crawlerfilestandardcolumnId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfilestandardcolumn", crawlerfilestandardcolumn);

		// 把所有的引用crawlerfilestandardcolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardcolumnService
				.loadCrawlerfilestandardcolumnObjectReferencedSet(crawlerfilestandardcolumn));

		// 将编辑页面crawlerfilestandardcolumn添加到mav中
		mav.setViewName("crawlerfilestandardcolumn/editCrawlerfilestandardcolumn.jsp");
		return mav;
	}

	@RequestMapping("/newCrawlerfilestandardcolumn")
	public ModelAndView newCrawlerfilestandardcolumn() {
		ModelAndView mav = new ModelAndView();
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = new Crawlerfilestandardcolumn();

		mav.addObject("crawlerfilestandardcolumn", crawlerfilestandardcolumn);

		// 把所有的引用crawlerfilestandardcolumn对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardcolumnService
				.loadCrawlerfilestandardcolumnObjectReferencedSet(crawlerfilestandardcolumn));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfilestandardcolumn/editCrawlerfilestandardcolumn.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawlerfilestandardcolumn")
	public ModelAndView saveCrawlerfilestandardcolumn(
			@ModelAttribute Crawlerfilestandardcolumn crawlerfilestandardcolumn) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandardcolumn existingCrawlerfilestandardcolumn = CmiHibernateProxyUtil
				.deproxy(crawlerfilestandardcolumnDAO
						.findCrawlerfilestandardcolumnByPhysicalPrimaryKey(crawlerfilestandardcolumn.getName()));

		// 新建记录
		if (crawlerfilestandardcolumn.getId() == null) {
			if (crawlerfilestandardcolumn.equals(existingCrawlerfilestandardcolumn)) {

				mav.addAllObjects(crawlerfilestandardcolumnService
						.loadCrawlerfilestandardcolumnClassReferencedSet(crawlerfilestandardcolumn));
				// mav = editContinue(mav, crawlerfilestandardcolumn);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandardcolumn/editCrawlerfilestandardcolumn.jsp");
			} else {
				crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
				mav.setViewName("forward:/listCrawlerfilestandardcolumns");
			}
		}
		// 编辑记录
		else {
			Crawlerfilestandardcolumn originCrawlerfilestandardcolumn = CmiHibernateProxyUtil
					.deproxy(crawlerfilestandardcolumnDAO
							.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumn.getId()));
			if (!crawlerfilestandardcolumn.equals(originCrawlerfilestandardcolumn)
					&& crawlerfilestandardcolumn.equals(existingCrawlerfilestandardcolumn)) {
				mav.addAllObjects(crawlerfilestandardcolumnService
						.loadCrawlerfilestandardcolumnClassReferencedSet(crawlerfilestandardcolumn));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandardcolumn/editCrawlerfilestandardcolumn.jsp");
			} else {
				crawlerfilestandardcolumnService.saveCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
				mav.setViewName("forward:/listCrawlerfilestandardcolumns");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfilestandardcolumnController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
